package easy;

public class Solution_541 {

    public String reverseStr(String s, int k) {
        int step = 2 * k;
        char[] chars = s.toCharArray();
        int count = (chars.length + - 1) / step + 1;

        for (int i = 0; i < count; i++) {
            int sub = chars.length - i * step;
            int size = Math.min(sub, k);
            int baseIndex = i * step;
            for (int j = 0; j < size/2; j++) {
                int start = baseIndex + j;
                int end = baseIndex + size - 1 - j;
                char c = chars[end];
                chars[end] = chars[start];
                chars[start] = c;
            }
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        Solution_541 model = new Solution_541();
        System.out.println(model.reverseStr("abcdefg", 2));
        System.out.println(model.reverseStr("abcd", 2));
    }
}
